package com.hkedou.yuanhuo.shiro;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.subject.support.WebDelegatingSubject;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class TokenAccessControlFilter extends AccessControlFilter {

    static final Logger logger= LoggerFactory.getLogger("com.hkedou.adminsystem.login");

    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        System.out.println("TokenAccessControlFilter.isAccessAllowed");
        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        System.out.println("TokenAccessControlFilter.onAccessDenied");


        //是否为登录主页,登录主页就忽略权限判定
        if(isLoginRequest(servletRequest,servletResponse)||this.pathsMatch("/login", servletRequest)) {
            saveRequest(servletRequest);
            return true;
        }

//        logger.error("",new Exception());
        Subject subject = getSubject(servletRequest, servletResponse);
        System.out.println("subject.class="+subject.getClass());
        boolean authenticated = subject.isAuthenticated();
        System.out.println("--------------authenticated="+authenticated);
        if(!authenticated){
            saveRequest(servletRequest);
            System.out.println("---------------tiaozhuan");
            super.redirectToLogin(servletRequest,servletResponse);
            return false;
        }
        return true;
    }
}
